package zuoye_3_24;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

//对sort.txt进行排序
public class Zuoye5 {
    public static void main(String[] args) {
        try {
            File inputFile = new File("sort.txt");
            File outputFile = new File("sorted.txt");

            BufferedReader reader = new BufferedReader(new FileReader(inputFile));
            BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile));

            ArrayList<String> lines = new ArrayList<>();
            String line;
            while ((line = reader.readLine()) != null) {
                // Exclude lines starting with "-"
                if (!line.trim().startsWith("-") && line.trim().length() > 0) {
                    lines.add(line);
                }
            }

            Collections.sort(lines);

            for (String sortedLine : lines) {
                System.out.println(sortedLine);
                writer.write(sortedLine);
                writer.newLine();
            }

            reader.close();
            writer.close();
            System.out.println("Sorting completed. Results written to sorted.txt.");
        } catch (IOException e) {
            System.err.println("Error reading or writing files: " + e.getMessage());
        }
    }
}